package indi.zn.commons.easiness;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;


public class DataSources {
	
	public static final  String[] dialectNames =new  String[]{"mysql","mariadb","oracle","sqlserver"};

	public static String getUrl(DataSource dataSource) throws SQLException {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            return conn.getMetaData().getURL();
        } catch (SQLException e) {
            throw new SQLException(e);
        } finally {
            if (conn != null) {
                try {
                   conn.close(); 
                } catch (SQLException e) {
                    //ignore
                }
            }
        }
    }
	
	public static String getSchema(DataSource dataSource) throws SQLException {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            return conn.getSchema();
        } catch (SQLException e) {
        	
        	e.printStackTrace();        	
            throw new SQLException(e);
        } finally {
            if (conn != null) {
                try {
                   conn.close(); 
                } catch (SQLException e) {
                    //ignore
                }
            }
        }
    }
	
	
	 public static String dialect(String jdbcUrl) {
	        for (String dialect : dialectNames) {
	            if (jdbcUrl.indexOf(":" + dialect + ":") != -1) {
	                return dialect;
	            }
	        }
	        return null;
	  }
	
	 
	 public static void main(String[] args) {
		
		 System.out.println(dialect("jdbc:mysql://192.168.199.207:3306/authbase?useUnicode=true&characterEncoding=utf8&autoReconnect=true"));
		 
	}
	 
	 
}
